package com.ssyan.factory.simplefactory.order;

import com.ssyan.factory.simplefactory.CheesePizza;
import com.ssyan.factory.simplefactory.GreekPizza;
import com.ssyan.factory.simplefactory.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author ssyan
 * @Description 订购披萨
 * @create 2021/8/27 22:15
 * @Version 1.0
 */
public class OrderPizza {

    Pizza pizza = null;
    String orderType; //订购披萨的类型

    //构造器
//    public OrderPizza() {
//        do {
//            orderType = getType();
//            if (orderType.equals("greek")) {
//                pizza = new GreekPizza();
//            } else if (orderType.equals("cheese")) {
//                pizza = new CheesePizza();
//            } else {
//                break;
//            }
//            //输出pizza制作过程
//            pizza.setName(orderType);
//            pizza.prepare();
//            pizza.bake();
//            pizza.cut();
//            pizza.box();
//        }while (true);
//    }

    //定义一个简单工厂对象
    private SimpleFactory simpleFactory;

    //构造器
    public OrderPizza(SimpleFactory simpleFactory) {
        setFactory(simpleFactory);
    }

    public void setFactory(SimpleFactory simpleFactory) {
        this.simpleFactory = simpleFactory;

        while (true){
            orderType = getType();
            pizza = this.simpleFactory.createPizza(orderType);

            //输出pizza
            if (pizza != null) {
                pizza.setName(orderType);
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println("订购披萨失败");
                break;
            }
        };
    }

    private String getType() {
        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza type:");
            String str = strin.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
